const express = require('express')
const app = express()

const indent = (n) => new Array(n).join('&nbsp;')

const mid1 = () => (req, res, next) => {
  res.body = '<h2>请求=> 第一层响应中间件</h2>'
  next()
  res.body += '<h2>响应<= 响应第一层中间件</h2>'
}

const mid2 = () => (req, res, next) => {
  res.body += `<h2>${indent(4)}请求=> 第二层响应中间件</h2>`
  next()
  res.body += `<h2>${indent(4)}响应<= 响应第二层中间件</h2>`
}

const mid3 = () => (req, res, next) => {
  res.body += `<h2>${indent(8)}请求=> 第三层响应中间件</h2>`
  next()
  res.body += `<h2>${indent(8)}响应<= 响应第三层中间件</h2>`
}

app.use(mid1())
app.use(mid2())
app.use(mid3())

app.get('/', (req, res, next) => {
  res.send(`${res.body}<h1 style="color: red">${indent(12)}Koa核心业务</h1>`)
})


app.listen(8000, () => console.log(8000))